package com.ifnk.portaljava.dapWeb.HandlerXML;
/**
 * <p>Title: 处理概要信息xml节点类</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author WenSheng.Song.hlyt@gmail.com
 * @version 1.0
 */

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;

public class ParseXML {
	public String parse(InputStream inn) throws IOException, DocumentException {
		SAXReader sax = new SAXReader();
		InputStreamReader strInStream = new InputStreamReader(inn, "GBK");
		Document document = sax.read(strInStream);
		// 将proto节点的名字改为field
		List pro = document.selectNodes("//proto");
		Iterator itr = pro.iterator();
		while (itr.hasNext()) {
			Element element = (Element) itr.next();
			element.setName("field");
		}
		String xml = null;
		List list = document.selectNodes("//field");
		Iterator it = list.iterator();
		int i = 1;
		while (it.hasNext()) {
			Element fieldElement = (Element) it.next();
			Attribute attr = fieldElement.attribute("showname");
			// 如果没有showname属性，则获取show属性值并把show属性改为shownane
			if (attr == null) {
				attr = fieldElement.attribute("show");
				if (attr != null) {
					fieldElement.addAttribute("showname", attr.getValue());
					fieldElement.remove(attr);
				}
			}
			fieldElement.addAttribute("id", "" + i++);
			Attribute hide = fieldElement.attribute("hide");
			if (hide != null && "yes".equals(hide.getValue())) {
				fieldElement.getParent().remove(fieldElement);
			}
		}
		// 删除packet节点
		Element packet = document.getRootElement().element("packet");
		List nodes = packet.elements();
		document.getRootElement().remove(packet);
		document.getRootElement().setContent(nodes);
		// 删除name=geninfo的节点
		Node geninfo = document.getRootElement().selectSingleNode(
				"/pdml/field[@name='geninfo']");
		document.getRootElement().remove(geninfo);

		List lists = document.selectNodes("//field");
		Iterator its = lists.iterator();
		while (its.hasNext()) {
			Element field = (Element) its.next();
			if (field.attribute("show") == null
					&& field.attribute("showname") == null) {
				List fiel = field.elements();
				Element first = (Element) fiel.get(0);
				List second = first.elements();
				List itor = first.attributes();
				field.setAttributes(itor);
				field.remove(first);
				field.setContent(second);
			}
		}
		xml = document.asXML();
		return xml;
	}

	public static void main(String[] args) throws Exception {
		ParseXML xml = new ParseXML();
		String cdrXml = "<CDR><protocol_type>11</protocol_type><ReportTime>  2011 15:30 12345656789  </ReportTime><svrid>000</svrid><StartTime>  2010 15:30 12345656789  </StartTime><device>0000</device></CDR>";
		String cdr = xml.parseCdrXml(2, cdrXml);
		System.out.println(cdr);
//		 InputStream in = new FileInputStream(new File("flex_src/erb1.xml"));
//		 try {
//		 String str = xml.parse(in);
//		 System.out.println(str);
//		 } catch (IOException e) {
//		 e.printStackTrace();
//		 } catch (DocumentException e) {
//		 e.printStackTrace();
//		 }
//		String time = "2010 15:30 123456";
//		int index = time.lastIndexOf(" ");
//		String str = time.substring(index);
//		time = time.substring(0,index);
//		System.out.println(index+" " + time + " " +str);
	}

	public String parseCdrXml(int number, String cdrXml) throws DocumentException {
		Document document = null;
		String xmlString = null;
		String str_StartTime;
		String str_StartTime_s;
		document = DocumentHelper.parseText(cdrXml);
		List cdrList = document.selectNodes("CDR");
		Element cdrElement = (Element) cdrList.get(0);
		cdrElement.addAttribute("id", String.valueOf(number));
		
		List list = document.getRootElement().selectNodes("StartTime");
		Element element = (Element) list.get(0);
		str_StartTime = element.getText().trim();
		int index = str_StartTime.lastIndexOf(" ");
		str_StartTime_s = str_StartTime.substring(index + 1);
		str_StartTime = str_StartTime.substring(0,index);
		element.setText(str_StartTime);
		document.getRootElement().addElement("StartTime_S").setText(str_StartTime_s);
		
		list = document.getRootElement().selectNodes("ReportTime");
		element = (Element) list.get(0);
		String reportTime = element.getText().trim();
		index = reportTime.lastIndexOf(" ");
		reportTime = reportTime.substring(0, index);
		element.setText(reportTime);
		xmlString = cdrElement.asXML();	
		if (number == 0) {
			return document.asXML();
		} else {
			return xmlString;
		}
	}
	
	
}
